package com.nuanshui.heatedloan.credit.pay;

import com.alibaba.dubbo.config.annotation.Reference;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.constant.GlobalParam;
import com.nuanshui.heatedloan.constant.RabbitMqQueue;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.constant.PayType;
import com.nuanshui.heatedloan.credit.facade.PayOrderInfoService;
import com.nuanshui.heatedloan.credit.facade.AllinPayNotifyService;
import com.nuanshui.heatedloan.credit.response.PayCallbackPara;
import com.nuanshui.heatedloan.fund.facade.TonlyTradeInfoService;
import com.nuanshui.heatedloan.fund.vo.TonlyTradeInfoVO;
import com.nuanshui.heatedloan.middleware.rabbitmq.RabbitmqMessageSender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zss
 * @date 2017/7/26 12:12
 * @description
 * @Since
 */
@Service(ServiceConstant.ALLINPAYNOTIFYSERVICE)
public class AllinPayNotifyServiceImpl implements AllinPayNotifyService {

    private Logger logger = LoggerFactory.getLogger(AllinPayNotifyServiceImpl.class);

    @Autowired
    private RabbitmqMessageSender rabbitmqMessageSender;

    @Autowired
    private PayOrderInfoService payOrderInfoService;

    @Reference(url = GlobalParam.FUND_DUBBO_SERVER, timeout = GlobalParam.DUBBO_SERVER_TIME_OUT,version = GlobalParam.FUND_DUBBO_SERVER_VERSION)
    private TonlyTradeInfoService tonlyTradeInfoService;

    @Override
    public String tradeSuccessNotify(AllinPayResultNotify allinPayResultNotify) {
        LogUtil.info(logger, "---------成功回调交易通知:"+ allinPayResultNotify.toString());

        if(allinPayResultNotify != null)
        {
            rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_DEAL_TRADE_ORDER, allinPayResultNotify);
        }
        return "";
    }

    @Override
    public String tradeFailNotify(AllinPayResultNotify allinPayResultNotify) {
        LogUtil.info(logger, "---------失败回调交易通知:"+ allinPayResultNotify.toString());
        TonlyTradeInfoVO tonlyTradeInfoVO = tonlyTradeInfoService.loadByReqSn(allinPayResultNotify.getBATCHID());
        if(tonlyTradeInfoVO != null){
                PayCallbackPara payCallbackPara = new PayCallbackPara();
                payCallbackPara.setOrderNo(tonlyTradeInfoVO.getOrderNum());
                payCallbackPara.setType(tonlyTradeInfoVO.getType() + "");
                payCallbackPara.setResultCode("00");
                rabbitmqMessageSender.send(RabbitMqQueue.RABBIT_MQ_DEAL_TRADE_ORDER, payCallbackPara, true);
        }else {
            LogUtil.info(logger, "---------处理订单状态失败，请进行交易明细查询----------");
        }
        return "";
    }

    @Override
    public String tradeRefundNotify(AllinPayResultNotify allinPayResultNotify) {
        logger.info("退款交易通知信息"+ allinPayResultNotify.toString());
        return "";
    }
}
